/*
** Copyright (c) 2010 by Lorimark Solutions, LLC.
** All Rights Reserved.
** Send inquiries to: info@lorimarksolutions.com
**
*/
/*
** write lfuse 0 0xe6 // 20mhz resonator
** write hfuse 0 0xd9 // turn off jtag interface
** write hfuse 0 0xd7 // eesave turned on
**

HFUSE    Bit Description Default
OCDEN(4) 7   Enable OCD  1 (unprogrammed, OCD disabled)
JTAGEN   6   Enable JTAG 0 (programmed, JTAG enabled)
SPIEN(1) 5   Enable Serial Program and Data Downloading
0 (programmed, SPI prog.
enabled)
WDTON(3) 4 Watchdog Timer always on 1 (unprogrammed)
EESAVE 3
EEPROM memory is preserved
through the Chip Erase
1 (unprogrammed, EEPROM
not preserved)
BOOTSZ1 2
Select Boot Size (see Table 24-9 for
details)
0 (programmed)(2)
BOOTSZ0 1
Select Boot Size (see Table 24-9 for
details)
0 (programmed)(2)
BOOTRST 0 Select Reset Vector 1 (unprogrammed)


*/

#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <evie-systems.h>
#include <font3x5.h>
#include <mega164serial.h>
#include <serial.h>
#include <packet.h>
#include <crc8.h>
#include "lcd.h"
#include "bargraph.h"

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
**  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
**
** USER INTERFACE PUSHBUTTONS
**
*/

#define UI_DDR DDRC
#define UI_OUT PORTC
#define UI_PIN PINC
#define UI_PB1 (1<<PC0)
#define UI_PB2 (1<<PC1)
#define UI_QU1 (1<<PC2)
#define UI_QU2 (1<<PC3)

volatile sint8  cmd;
volatile uint8  moduleIndex;
volatile uint8  moduleNode;
volatile sint8  quadValue;
volatile uint16 totalPackCurrentVoltage;
volatile uint16 totalPackMaxVoltage;
volatile uint16 totalPackMinVoltage;
volatile uint16 totalPackCurrentAmps;
volatile uint16 totalPackMaxAmps;
volatile uint16 totalPackMinAmps;
volatile uint16 totalMotorCurrentAmps;
volatile uint16 totalMotorMaxAmps;
volatile uint16 totalMotorMinAmps;
volatile uint16 auxBatteryCurrentTemp;
volatile uint16 auxBatteryMaxTemp;
volatile uint16 auxBatteryMinTemp;
volatile uint16 auxBatteryCurrentVolts;
volatile uint16 auxBatteryMaxVolts;
volatile uint16 auxBatteryMinVolts;


const uint8   nodeName[8]  EEMEM = "LCD01";
const uint16  nodeSerial   EEMEM = 1000;
const uint8   nodeIDs[4]   EEMEM = { 250, 255, 255, 255 };
const uint8   nodePad[18]  EEMEM = { 255, 255 };


/*
** define only *one* of the following:
**
*/
//#define BENCH_TEST
#define JOHNS_FIERO
//#define BATT_MOWER


#ifdef BATT_MOWER

#define TEMPERATURE_NODE 1

#define MODULE_COUNT 1
#define CELL_COUNT   3
#define VIN_LOW      8000
#define VIN_HIGH     16000

const mvc_t mvc[] =
{
// | module  | cell             | cell
// | address | count            | index
  {  1,        CELL_COUNT,        0         }, // 0
  {  0,        0,                 0         }, // 1
  {  0,        0,                 0         }, // 2
  {  0,        0,                 0         }, // 3
  {  0,        0,                 0         }, // 4
  {  0,        0,                 0         }, // 5
  {  0,        0,                 0         }, // 6
  {  0,        0,                 0         }, // 7
  {  0,        0,                 0         }
};
#endif


#ifdef BENCH_TEST

#define MODULE_COUNT 1
#define CELL_COUNT   32
#define VIN_LOW      2600
#define VIN_HIGH     3800

const mvc_t mvc[] =
{
// | module  | cell  | cell
// | address | count | index
  { 1,        8,       0         }, // 0
  { 0,        0,       0         }, // 1
  { 0,        0,       0         }, // 2
  { 0,        0,       0         }, // 3
  { 0,        0,       0         }, // 4
  { 0,        0,       0         }, // 5
  { 0,        0,       0         }, // 6
  { 0,        0,       0         }, // 7
  { 0,        0,       0         }
};
#endif


/*
** Pack Voltage Monitor for John's fiero
**
** John's Fiero has 8 of these modules, and
**  each module monitors 4 cells per.
**
*/
#ifdef JOHNS_FIERO

#define TEMPERATURE_NODE 6

#define MODULE_COUNT 7
#define CELL_COUNT   32
#define VIN_LOW  2600
#define VIN_HIGH 3800

const mvc_t mvc[] =
{
// | module  | cell  | cell
// | address | count | index
  { 1,        4,       0         }, // 0
  { 2,        4,       4         }, // 1
  { 3,        4,       8         }, // 2
  { 4,        4,      12         }, // 3
  { 5,        4,      16         }, // 4
  { 6,        4,      20         }, // 5
  { 7,        8,      24         }, // 6
  { 0,        0,       0         }, // 7
  { 0,        0,       0         }
};
#endif




/*
** Traction Cell Voltage
**
*/
volatile uint16 tmt[MODULE_COUNT];
volatile tcv_t  tcv[CELL_COUNT];



void init_ui(void)
{
  /*
  ** Apply the pullups to the pins
  **
  */
  UI_OUT   |=
    UI_PB1 |
    UI_PB2 |
    UI_QU1 |
    UI_QU2 ;

  /*
  ** Make the DDR inputs for the buttons
  **
  */
  UI_DDR      &=
    ~(UI_PB1  |
      UI_PB2  |
      UI_QU1  |
      UI_QU2) ;

  /*
  ** Set the quad value to nothing.
  **
  */
  quadValue = 0;

}


sint8 quadTurns()
{
  sint8 retVal = quadValue;
  quadValue    = 0;
  return( retVal );
}









/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
**  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
**
** 
**
*/



#define PIZ (1<<PC6)

#define LED (1<<PC4)
#define DB1 (1<<PC3)
#define DB2 (1<<PC2)

#define FLPDB1 PORTD ^= DB1
#define TOGDB1 FLPDB1; /* _delay_us(1); */ FLPDB1

#define FLPDB2 PORTD ^= DB2
#define TOGDB2 FLPDB2; /* _delay_us(1); */ FLPDB2

#define LED_FLASH_LONG  250
#define LED_FLASH_SHORT 15

uint16 ledFlash;
uint8  ledState;

void led_on()
{
  PORTC |=  LED;
}

void led_off()
{
  PORTC &= ~LED;
}

uint8 lastQuad;

ISR(TIMER1_OVF_vect)
{
  uint8 thisQuad = UI_PIN & (UI_QU1 | UI_QU2);
  if( lastQuad != thisQuad )
  {
    if( lastQuad == 0x00 )
    {
      if( thisQuad == 0x04 ) quadValue--;
      if( thisQuad == 0x08 ) quadValue++;
    }

    if( lastQuad == 0x04 )
    {
      if( thisQuad == 0x0c ) quadValue--;
      if( thisQuad == 0x00 ) quadValue++;
    }

    if( lastQuad == 0x08 )
    {
      if( thisQuad == 0x00 ) quadValue--;
      if( thisQuad == 0x0c ) quadValue++;
    }

    if( lastQuad == 0x0c )
    {
      if( thisQuad == 0x08 ) quadValue--;
      if( thisQuad == 0x04 ) quadValue++;
    }

    lastQuad = thisQuad;
  }

  if( !ledFlash-- )
  {
//    PORTD ^= LED;

    if( ledState )
    {
      led_on();
      ledFlash  = LED_FLASH_SHORT;
      ledState  = 0;
    }
    else
    {
      led_off();
      ledFlash  = LED_FLASH_LONG;
      ledState  = 1;
    }
  }
}

void init_timer(void)
{

  sei();

  TCCR1A = 
    (0b00   << COM1A0) | // 6.Compare Output Mode for A
    (0b00   << COM1B0) | // 4.Compare Output Mode for B 
    (0b00   << WGM10)  ; // 0.Waveform Generation Mode

  TCCR1B = 
    (0b0    << ICNC1)  | // 7.Input Capture Noise Canceler
    (0b0    << ICES1)  | // 6.Input Capture Edge Select
    (0b00   << WGM12)  | // 3.Waveform Generation Mode
    (0b001  << CS10)   ; // 0.Clock Select (1=no prescaling)

  TCCR1C =
    (0b0    << FOC1A)  | // 7.Force Output Compare for A
    (0b0    << FOC1B)  ; // 6.Force Output Compare for B

  TIMSK1 = 
    (0b0    << ICIE1)  | // 5.Input Capture Interrupt Enable
    (0b0    << OCIE1B) | // 2.Output Compare B Match Interrupt Enable
    (0b0    << OCIE1A) | // 1.Output Compare A Match Interrupt Enable
    (0b1    << TOIE1)  ; // 0.Overflow Interrupt Enable

//  OCR1AH = (20000>>8);
//  OCR1AL = (20000&255);

}


/*************************************************************************/
/*************************************************************************/
/*************************************************************************/

void showBattery( uint8 bi )
{
//  uint8 i = bi;
//
//  bi &= 0b00000111;
//
//  battery = 
//    (packet.dat[(bi*2)+0])   +
//    (packet.dat[(bi*2)+1]*256);
//
//  if( batteries[i].current != battery || 
//      batteries[i].status  != 1          )
//  {
//    batteries[i].status  = 1;
//    batteries[i].last    = batteries[i].current;
//    batteries[i].current = battery;
//
////    if( (bi & 0b00000100) == 0b00000000 )
////      writeDecNumber_lcd( (20*i)+128, battery, 1000 );
//
////    if( (bi & 0b00000100) == 0b00000100 )
////      writeDecNumber_lcd( (20*i)+176, battery, 1000 );
//
//    if( battery > VOFFSET )
//    {
//      battery -= VOFFSET;
//      battery /= 40;
//    }
//    else
//    {
//      battery = 0;
//    }
//
//    drawBar( i, battery );
//
//  }
}

void showBatteries(void)
{
//  uint8 bi = (packet.src - 1) * CELL_COUNT; 
//  uint8 i;
//
//  for( i = 0; i < CELL_COUNT; i++ )
//  {
//    showBattery( bi + i );
//  }
//
}

void zapBatteries(void)
{
//  uint8 bi;
//
//  for( bi = 0; bi < CELL_COUNT; bi++ )
//  {
//    drawBar( bi, 0 );
//  }
}


void ShowBarGraph(void)
{
  uint8  i;
  uint16 battery;
  uint16 min, max;

  for( i=0; i<CELL_COUNT; i++ )
  {
    if( tcv[i].status == 1 )
    {
      battery = tcv[i].current;

      if( battery != tcv[i].last )
      {
        if( battery > VIN_LOW )
        {
          battery -= VIN_LOW;
          battery /= (VIN_HIGH-VIN_LOW)/40;
        }
        else
        {
          battery = 0;
        }


        min = tcv[i].min;
        if( min > VIN_LOW )
        {
          min -= VIN_LOW;
          min /= (VIN_HIGH-VIN_LOW)/40;
        }


        max = tcv[i].max;
        if( max > VIN_LOW )
        {
          max -= VIN_LOW;
          max /= (VIN_HIGH-VIN_LOW)/40;
        }


        drawBar( i, battery, min, max );

        tcv[i].last = battery;

      } // battery != last

    } // status == 1

    /*
    ** Here we have a bad PVM or something, and our data is
    **  bad, so we should indicate that somehow on the display.
    **
    */
//    else // voltage status is bad
//    {
//      drawBar( i, 0 );
//    }

  } // for( all cells )

} // endvoid ShowBarGraph(void)


void ShowRawAD(void)
{
  uint8  i;
  uint16 battery;

  for( i=0; i<CELL_COUNT; i++ )
  {
    if( tcv[i].status == 1 )
    {
      battery = tcv[i].current;

//      if( battery != tcv[i].last )
      {
        writeDecNumberVert_lcd( i * 4 + 512, battery, 1000 );
      }

      tcv[i].last = battery;

    }

  }

}

/*************************************************************************/
/*************************************************************************/
/*************************************************************************/

uint8 ModuleIndex( uint8 node )
{
  uint8 i;

  for( i=0; i<MODULE_COUNT; i++ )
  {
    if( mvc[i].node == node )
    {
      return( i );
    }
  }

  return( -1 );
}

void ProcessMVC(void)
{
  uint8 mi = ModuleIndex(packet.src);

  if( mi == -1 ) return;

  /*
  ** Fetch the number of cells in this module
  **
  */
  uint8 i = mvc[mi].cells;

  /*
  ** Fetch the bar-graph cell index
  **
  */
  uint8 ci = mvc[mi].tci + i;

  /*
  ** Get the traction module temperature
  **
  */
  tmt[mi] = 
    (packet.dat[0])   +
    (packet.dat[1]*256);

  /*
  ** Pull every cell from the packet array
  **  and stuff the values into the traction
  **  cell voltage array.
  **
  */
  while( i )
  {
    i--;
    ci--;

    tcv[ci].current =
      (packet.dat[(i*2)+2])   +
      (packet.dat[(i*2)+3]*256);

    tcv[ci].status = 1;

  }

}


void ProcessMin(void)
{
  uint8 mi = ModuleIndex(packet.src);

  if( mi == -1 ) return;

  /*
  ** Fetch the number of cells in this module
  **
  */
  uint8 i = mvc[mi].cells;

  /*
  ** Fetch the bar-graph cell index
  **
  */
  uint8 ci = mvc[mi].tci + i;

  /*
  ** Pull every cell from the packet array
  **  and stuff the values into the traction
  **  cell voltage array.
  **
  */
  while( i-- )
  {
    tcv[--ci].min =
      (packet.dat[(i*2)+0])   +
      (packet.dat[(i*2)+1]*256);
  }

}

void ProcessMax(void)
{
  uint8 mi = ModuleIndex(packet.src);

  if( mi == -1 ) return;

  /*
  ** Fetch the number of cells in this module
  **
  */
  uint8 i = mvc[mi].cells;

  /*
  ** Fetch the bar-graph cell index
  **
  */
  uint8 ci = mvc[mi].tci + i;

  /*
  ** Pull every cell from the packet array
  **  and stuff the values into the traction
  **  cell voltage array.
  **
  */
  while( i-- )
  {
    tcv[--ci].max =
      (packet.dat[(i*2)+0])   +
      (packet.dat[(i*2)+1]*256);
  }

}


void ProcessAuxAmps(void)
{
  uint8 mi = ModuleIndex(packet.src);

  if( mi == -1 ) return;

  auxBatteryCurrentTemp  = 
    (packet.dat[0])      +
    (packet.dat[1]*256)  ;

  auxBatteryCurrentVolts =
    (packet.dat[2])      +
    (packet.dat[3]*256)  ;

  totalPackCurrentAmps   =
    (packet.dat[4])      +
    (packet.dat[5]*256)  ;

  totalMotorCurrentAmps  = 
    (packet.dat[6])      +
    (packet.dat[7]*256)  ;

}

void ProcessTest(void)
{
  uint8 mi = ModuleIndex(packet.src);

  if( mi == -1 ) return;

//  writeDecNumber_lcd( 128, packet.dat[0] + (packet.dat[1] * 256), 100 );
//  writeHexNumber_lcd( 128, packet.dat[0] + (packet.dat[1] * 256) );


}


uint8 ProcessNetworkResponse(void)
{
  if( !rxNetworkPacket() ) return( 0 );

  /* * * * * * * * * * * * * * * * * * * * * * *
  ** At this point we have a valid packet and it
  **  is addressed to us.  We can now process the
  **  data in it and do whatever is requested.
  **
  */

  /*
  ** Switch on the command
  **
  */
  switch( packet.cmd )
  {
    /*
    ** read out raw data
    **
    */
    case 20:
    {
      ProcessMVC();
      return(1);
    }

    /*
    ** read out scaled data
    **
    */
    case 21:
    {
      ProcessMVC();
      return(1);
    }

    case 23:
    {
      ProcessMin();
      return(1);
    }

    case 24:
    {
      ProcessMax();
      return(1);
    }

    case 35:
    {
      ProcessAuxAmps();
      return(1);
    }

    case 250:
    {
      ProcessTest();
      return(1);
    }

  }

  return(0);

}



void DisconnectModule(void)
{
  /*
  ** Fetch the number of cells in this module
  **
  */
  uint8 i = mvc[moduleIndex].cells;

  /*
  ** Fetch the bar-graph cell index
  **
  */
  uint8 ci = mvc[moduleIndex].tci + i;

  /*
  ** Set the status on each cell to 0
  **
  */
  while( i-- )
  {
    tcv[--ci].status = 0;
  }

}


uint8 WaitForAnswer()
{
  uint8 timeout = 9;

  while( !ProcessNetworkResponse() && --timeout )
  {
    _delay_ms(2);
  }

  if( !timeout )
  {
    DisconnectModule();
//    writeDecNumber_lcd( (moduleIndex * 128) + 12, timeout,     10 );
//    writeDecNumber_lcd( (moduleIndex * 128) + 28, moduleIndex, 10 );
  }

  return( timeout );

}




/*************************************************************************/
/*************************************************************************/
/*************************************************************************/

void ShowPackVoltage()
{
  uint8  i;





#ifdef NEVER
  totalPackMaxVoltage = 0;
  for( i = 0; i < CELL_COUNT; i++ )
  {
    totalPackMaxVoltage += tcv[i].max / 100;
  }

  writeHugeDecNumber_lcd( 46, totalPackMaxVoltage / 10, 100, -1 );
#endif






#ifdef NEVER
  totalPackMinVoltage = 0;
  for( i = 0; i < CELL_COUNT; i++ )
  {
    totalPackMinVoltage += tcv[i].min / 100;
  }

  writeBigDecNumber_lcd( 72, totalPackMinVoltage / 10, 100, -1 );
#endif





#ifndef NEVER

  writeHugeDecNumber_lcd( 76, totalPackCurrentAmps, 100, -1 );
  writeHugeCharacter_lcd( 'A' );

//    writeDecNumber_lcd( 70,     auxBatteryCurrentVolts, 10000 );
//    writeDecNumber_lcd( 100,    totalPackCurrentAmps,   10000 );
//    writeDecNumber_lcd( 70+128, totalMotorCurrentAmps,  10000 );

#endif


#ifdef NEVER
//  writeDecNumber_lcd( 64, tmt[0], 10000 );
  if( (tmt[TEMPERATURE_NODE-1] < 1500) && 
      (tmt[TEMPERATURE_NODE-1] > 0  )  )
  {
    writeHugeDecNumber_lcd( 69, tmt[TEMPERATURE_NODE-1], 1000, 3 );
    writeHugeCharacter_lcd( 'F' );
  }

#endif



#ifndef NEVER
  totalPackCurrentVoltage = 0;
  for( i = 0; i < CELL_COUNT; i++ )
  {
    totalPackCurrentVoltage += tcv[i].current / 100;
  }

  writeHugeDecNumber_lcd( 0, totalPackCurrentVoltage, 1000, 3 );
  writeHugeCharacter_lcd( 'V' );
#endif



}


/*************************************************************************/
/*************************************************************************/
/*************************************************************************/

/*
** Send a command to a node and wait for an answer.  If an answer doesn't
**  come, then retry.
**
*/
void SendCommand( uint8 targetNode, uint8 cmd )
{
  uint8 retry = 3;

//  writeDecNumber_lcd( 128 + (moduleIndex*24),     moduleIndex,       1 );
//  writeDecNumber_lcd( 132 + (moduleIndex*24), svm[moduleIndex].node, 1 );
//  writeDecNumber_lcd( 128 + (moduleIndex*24),     cmd,       10 );

  while( retry-- )
  {
    crc8Result = 0;
    txPut( ':'                             ); // atn
    txPut( targetNode                      ); // dst
    txPut( eeprom_read_byte(&nodeIDs[0])   ); // src
    txPut( 0                               ); // status
    txPut( cmd                             ); // cmd
    txPut( 6                               ); // len

    txPut( totalPackCurrentVoltage  &  255 ); // pack information
    txPut( totalPackCurrentVoltage  >> 8   );
    txPut( totalPackCurrentAmps     &  255 );
    txPut( totalPackCurrentAmps     >> 8   );
    txPut( totalMotorCurrentAmps    &  255 );
    txPut( totalMotorCurrentAmps    >> 8   );

    txPut( crc8Result               ); // crc

    if( WaitForAnswer() ) return;
  }

}

/*************************************************************************/
/*************************************************************************/
/*************************************************************************/

int main(void)
{
  uint8 display_mode = 1;
//  uint8 read_max = 10;
  uint8 i;

  DDRC |= PIZ;
  DDRC |= LED;
  DDRC |= DB1;
  DDRC |= DB2;

//  DDRD |= 0b00100000; // lcd backlight

  led_off();

  /*
  ** Initialize the world
  **
  */
  init_ui();
  init_timer();
  init_lcd(1);
  init_usart();

  puts_lcd( "evie-systems LCD164-01" );

  totalPackCurrentVoltage = 0;
  totalPackMaxVoltage     = 0;
  totalPackMinVoltage     = 0;
  totalPackCurrentAmps    = 0;
  totalPackMaxAmps        = 0;
  totalPackMaxAmps        = 0;
  totalMotorCurrentAmps   = 0;
  totalMotorMaxAmps       = 0;
  totalMotorMinAmps       = 0;

  cmd = 21;

  /*
  ** Let'r rip!
  **
  */
  ENABLE_INTERRUPTS;

  _delay_ms(3000);

  fillScreen_lcd(0x00);            // Clear the display

  /*
  ** Get the LCD display formatted.  First print out all the
  **  numbers so that we know approximately what cell number
  **  we're looking at with a particular bar.  Then, print
  **  some little tick-marks so that we know what bar exactly
  **  the number is centered over.  Then print a dividing line
  **  so that we know where the top of the scale is.
  **
  **
  */
  writeDecNumber_lcd( 256 +  0,   1,  1 );
  writeDecNumber_lcd( 256 + 16,   5,  1 );
  writeDecNumber_lcd( 256 + 34,  10, 10 );
  writeDecNumber_lcd( 256 + 54,  15, 10 );
  writeDecNumber_lcd( 256 + 74,  20, 10 );
  writeDecNumber_lcd( 256 + 94,  25, 10 );
  writeDecNumber_lcd( 256 + 114, 30, 10 );

  pixel_lcd( 1,   0x16, 1 );
  pixel_lcd( 17,  0x16, 1 );
  pixel_lcd( 37,  0x16, 1 );
  pixel_lcd( 57,  0x16, 1 );
  pixel_lcd( 77,  0x16, 1 );
  pixel_lcd( 97,  0x16, 1 );
  pixel_lcd( 117, 0x16, 1 );

  for( i=0; i<128; i++ )
  {
    pixel_lcd( i, 0x17, 1 );
  }

  /*
  ** Initialize all the batteries so that they produce
  **  some sort of activity on initialization.
  **
  */
  for( i=0; i<CELL_COUNT; i++ )
    tcv[i].status = -1;

  /*
  ** Reset all the module max readings
  **
  */
//  for( moduleIndex=0; moduleIndex<MODULE_COUNT; moduleIndex++ )
//  {
//    SendCommand( mvc[moduleIndex].node, 43 );
//    SendCommand( mvc[moduleIndex].node, 44 );
//  }

  /*
  ** Main Control Loop
  **
  */
  forever
  {

    /*
    ** Read all the voltage modules.
    **
    */
    for( moduleIndex=0; moduleIndex<MODULE_COUNT; moduleIndex++ )
    {
      SendCommand( mvc[moduleIndex].node, 21 );
    }

    /*
    ** Read the current sensing module.
    **
    */
    SendCommand( 20, 35 );


    /*
    ** Update the display
    **
    */
    switch( display_mode )
    {
      case 1:
      {
        ShowBarGraph();
        ShowPackVoltage();
        break;
      }

      case 2:
      {
        ShowRawAD();
        break;
      }
    }




//    writeDecNumber_lcd( 0, tcv[0].min, 10000 );
//    writeDecNumber_lcd( 0, cmd, 10 );

//    cmd += quadTurns();

//    if( cmd < 20 ) cmd = 20;
//    if( cmd > 21 ) cmd = 21;

#ifdef NEVER
    if( !read_max-- )
    {
      for( moduleIndex=0; moduleIndex<MODULE_COUNT; moduleIndex++ )
      {
        SendCommand( mvc[moduleIndex].node, 23  );
        SendCommand( mvc[moduleIndex].node, 24  );
        SendCommand( mvc[moduleIndex].node, 250 );
      }

      read_max = 100;
    }
#endif


//    if( ++totalPackCurrentAmps > 10000 )
//          totalPackCurrentAmps = 0;


//    UDR1 = 0x55;


#ifdef NEVER

  for( i=0; i<100; i++ )
  {
    PORTC |= PIZ;
    _delay_us(500);
    PORTC &= ~PIZ;
    _delay_us(500);
  }

#endif


#ifdef NEVER

    PORTD |=  0b00100000;
    _delay_us(2);
    PORTD &= ~0b00100000;
    _delay_us(9);

#endif

#ifdef NEVER
    x += quadTurns();

    writeHexNumber_lcd( 0,   lastQuad );
    writeHexNumber_lcd( 128, x        );

    drawBar( 5, x );
#endif

#ifdef NEVER
    for( target=0; target<32; target++ )
    {
      drawBar( target, x );

      x++;
      if( x > 32 ) x = 0;

    }

    _delay_ms(10);
#endif


  }



}


#ifdef NEVER

  for( ;; )
  {
    PORTD |=  0b00100000;
    _delay_us(2);
    PORTD &= ~0b00100000;
    _delay_us(9);
  }


#endif
